package com.zmp.kotlinnote.exchange

import android.graphics.Canvas
import android.view.View
import com.zmp.kotlinnote.entry.DPerson
import org.junit.Test

/**
 *@author zmp
 *Created by zmp on 2018/12/24
 *TODO kotlin包提供的扩展函数
 */

class Test5 {
    @Test
    fun mainTODO() {
        //----------------TODO 函数 --------------------------------
        object : View.OnClickListener {
            override fun onClick(v: View?) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
        }
    }

    //----------------with 函数 --------------------------------
    @Test
    fun mainWith() {
        val dPerson = DPerson()
        with(dPerson) {
            jump()
            work()
            name
        }.log()

        with(dPerson, fun DPerson.(): String {
            jump()
            return name
        }).log()

        Canvas().let { it->

        }
    }
    //----------------run 函数 --------------------------------

    @Test
    fun mainRun() {
        //普通函数
        run(fun(): Int {
            return 1
        }).log()

        //扩展函数
        val dPerson = DPerson()
        dPerson.run{
            jump()
            work()
            name.let {
                this
            }
        }.log()

        dPerson.run(fun DPerson.(): String {
            jump()
            return name
        }).log()

        println("arrayOf.forEach-----------------------------------start")
        val arrayOf = arrayOf(1, 2, 3, 4, 5)
    kotlin.run {
        arrayOf.forEach {
            if (this==3){
                return@run
            }

            print("aa")
        }
    }




//        arrayOf.forEach {
////            if (it == 3) {
////                return
////            }
////            println(it)
////        }
        for (it in arrayOf) {
            if (it == 3) {
                break
            }
            println(it)
        }

        println("arrayOf.forEach-----------------------------------------end")
    }

    //----------------apply 函数 --------------------------------
    @Test
    fun mainApply() {
        //扩展函数
        val dPerson = DPerson()
        dPerson.apply {
            jump()
            work()
        }.log()

        dPerson.apply(fun DPerson.():Unit {
            jump()
            work()
        }).log()
    }

    //----------------also 函数 --------------------------------
    @Test
    fun mainAlso() {
        //扩展函数
        val dPerson = DPerson()
        dPerson.also { it->
            it.jump()
            it.work()
            it.also { other->
                other.jump()
            }

        }.log()

        dPerson.also(fun(dPerson) {
            dPerson.jump()
            dPerson.work()
        }).log()
    }

    //----------------let 函数 --------------------------------
    @Test
    fun mainLet() {
        //扩展函数
        val dPerson = DPerson()
        dPerson.let {
            it.jump()
            it.work()
            it.name
        }.log()

        dPerson.let(fun(dPerson):String {
            dPerson.jump()
            dPerson.work()
            return dPerson.name
        }).log()
    }


    public inline fun <T> Array<out T>.forEach(action: T.() -> Unit){
        for (element in this) element.action()
    }
}